package com.weixing.mall.provider.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.weixing.mall.provider.mapper.SpecMapper;
import com.weixing.mall.provider.model.domain.Spec;
import com.weixing.mall.provider.model.dto.SpecDto;
import com.weixing.mall.provider.model.query.SpecQuery;
import com.weixing.mall.provider.model.vo.SpecVo;
import com.weixing.mall.provider.service.ISpecService;
import com.weixing.mall.provider.service.ISpecValueService;
import com.weixing.mall.provider.support.excel.ColorAndSizeData;
import com.weixing.mall.provider.support.excel.ColorAndSizeDataListener;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @ClassName SpecServiceImpl
 * @Description TODO <br/>
 * @Author gjq
 * @Modified By:
 * @Date 2019/8/9
 * @Version 1.0
 **/
/**
 * <p>
 * 商品模块-商品规格表 服务实现类
 * </p>
 *
 * @author Small
 * @since 2019-11-20
 */
@Service
public class SpecServiceImpl extends ServiceImpl<SpecMapper, Spec> implements ISpecService {
    @Autowired
    ISpecValueService specValueService;
    @Override
    public void save(SpecDto specDto) {
        Spec spec = new Spec();
        BeanUtils.copyProperties(specDto, spec);
        spec.setIsEnable(specDto.getIsEnable().getValue());
        if (null == specDto.getId()){
            save(spec);
        }else{
            updateById(spec);
        }
    }

    @Override
    public List<SpecVo> selectList(SpecQuery specQuery) {
        return baseMapper.selectList(specQuery);
    }


    @Override
    public Long  selectByName(String name) {
        return baseMapper.selectByName(name);
    }

    @Override
    public void init(MultipartFile file) {
        try{
            EasyExcel.read(file.getInputStream(), ColorAndSizeData.class, new ColorAndSizeDataListener(specValueService)).sheet().doRead();
        }catch (Exception e){

        }
    }


}
